<?php

namespace app\agentapi\model;
use app\common\exception\BaseException;
use think\Db;

/**
 *商户模型类
 * Class Shop
 * @package app\storeapi\model
 */
class Shop extends Base
{

    protected $name = 'store_shop';

    /**
     * 门店主页
     * @param $wxapp_id
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function index($wxapp_id)
    {
        //商家中心基本信息
        $info = $this->alias('a')
            ->join('yoshop_upload_file b','a.logo_image_id=b.file_id','left')
            ->field('a.shop_id,a.shop_name,b.storage,b.file_url,b.file_name')
            ->where(['a.wxapp_id'=>$wxapp_id])
            ->find();
        //处理图片
        $info['info'] = $this->getFilePathAttr($info);
        //订单
        $order = $this->commomOrder($wxapp_id);
        //访客记录
        $visitor = $this->visitor($wxapp_id);
        $data = compact('info','order','visitor');
        return $data;
    }
    //普通订单记录
    private function commomOrder($wxapp_id){
        // 筛选条件
        $filter = [];
        $filter['wxapp_id'] = $wxapp_id;
        $filter['pay_status'] = self::ORDER_PAY_PENDING;
        //普通订单
        $order = db('order')
            ->where($filter)
            ->field(['sum(pay_price)' => 'total','count(*)'=> 'count'])
            ->whereTime('pay_time','today')
            ->find();
        $order['total'] = $order['total']?:0;
        return $order;
    }

    //访客记录
    private function visitor($wxapp_id){
        //累计访客
        $allCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->count();

        //近7天访客
        $sevenCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->whereTime('visitor_time','-7 days')->count();
        //昨日访客
        $yesCount = db('store_shop_visitor')->where(['wxapp_id'=>$wxapp_id])->whereTime('visitor_time','yesterday')->count();

        return compact('allCount','sevenCount','yesCount');
    }


    /**
     * 店铺信息
     * @param $wxapp_id
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function storeInfo($wxapp_id)
    {
        //商家中心基本信息
        $data = $this->alias('a')
            ->join('yoshop_upload_file b','a.logo_image_id=b.file_id','left')
            ->join('yoshop_region c','a.province_id=c.id','left')
            ->join('yoshop_region d','a.city_id=d.id','left')
            ->join('yoshop_region e','a.region_id=e.id','left')
            ->field('a.shop_id,a.shop_name,a.linkman,a.phone,a.summary,a.province_id,a.city_id,a.region_id,a.address,a.longitude,a.latitude,c.name as province_name,d.name as city_name,e.name as region_name,b.storage,b.file_url,b.file_name')
            ->where(['a.wxapp_id'=>$wxapp_id])
            ->find();

        //处理图片
        $data['info'] = $this->getFilePathAttr($data);

        return $data;
    }


    /**
     * 编辑店铺信息
     * @param $params
     * @return \app\storeapi\model\store\Shop|null
     * @throws \think\exception\DbException
     */
    public function editInfo($params)
    {
        $flag = $this->allowField(true)->isUpdate(true,['wxapp_id'=>$params['wxapp_id']])->save($params);
        return $flag;

    }


}
